package pages;

import core.BrowserTypes;
import core.TestBase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import sun.rmi.runtime.Log;
import utils.Log4Test;

import java.util.List;

/**
 * Created by gzhovnuvaty on 7/10/14.
 */
public class AppleNotePage extends TestBase {
    protected By isAppleLocator = By.xpath("//div[@class='title-page-with-filters']/h1");
    protected WebElement isAppleLocatorPresent;

    protected By ratingDdl = By.xpath("//div[@id='sort_view']/a");
    protected WebElement clickRatingDdl;

    protected By expensiveToCheap = By.xpath("//ul[@class='m-sort']/li[2]");
    protected WebElement clickExpensiveToCheap;

//    protected By verifyMacBookZ0pu002je = By.xpath("//*[@class='gtile-i-title']/a[@href='http://rozetka.com.ua/apple_macbook_pro_retina_15_z0pu002je/p699984/']");
//    protected WebElement clickVerifyMacBookZ0pu002je;
//
//    protected By toCompare = By.xpath("//*[@id='wishlist-popup-699984']/preceding-sibling::div/label/input");
//    protected WebElement clickToCompare;

    protected By blockCompareList = By.xpath("//div[@class='block']/h3");
    protected WebElement verifyCompareList;


    protected By listOfCompareList = By.xpath("//div[@class='block']/ul/li/a[contains(@href, 'rozetka')]");
    protected List<WebElement> listOfCompare;


    protected By listOfTitelsList = By.xpath("//div[@class='gtile-i-title']/a");
    protected List<WebElement> listOfTitles;



    protected By compareCheckBox = By.xpath("./parent::div/following::div[@class='g-tools']/div[@name='comparison_new_catalog']/label/input");

    protected By compareLink = By.xpath("//div[@class='block']/a");
    protected WebElement compareLinkClick;

    protected By secondItemInCompareBlockIsVisible = By.xpath("//div[@class='block']/ul/li[2]/a[2]");



    public void clickCompareLink(){
        Log4Test.info("Click compare link");
        //WebDriverWait wait = new WebDriverWait(webDriver, 10);
        //wait.until(ExpectedConditions.visibilityOf(compareLinkClick));
        compareLinkClick = webDriver.findElement(compareLink);
        compareLinkClick.click();
    }






    public void setCheckBoxOnCompareBoxForMe2994Uaa() {
        Log4Test.info("Set ME2994Uaa checkbox");
        listOfTitles = webDriver.findElements(listOfTitelsList);
        for (WebElement elem : listOfTitles) {
            if (elem.getText().equals("Apple MacBook Pro Retina 15\" (ME294UA/A) Официальная гарантия!")) {
                WebElement checkBox = elem.findElement(compareCheckBox);
                checkBox.click();

//                try {
//                    Thread.sleep(3000);
//                } catch (InterruptedException ie1) {
//                    ie1.printStackTrace();
//                }

                break;


            }
        }
    }

//            else{
//                WebElement nextItems = webDriver.findElement(By.xpath("//span[@class='hidden']/following-sibling::a"));
//                nextItems.click();
//                for (WebElement ele: listOfTitles){
//                    if (ele.getText().equals("Apple MacBook Pro Retina 15\" (ME294UA/A) Официальная гарантия!")){
//                        WebElement checkBo = ele.findElement(compareCheckBox);
//                        checkBo.click();
//                        try {
//                            Thread.sleep(3000);
//                        }catch (InterruptedException ie1){
//                            ie1.printStackTrace();
//                        }
//
//                        break;
//
//                    }
//
//                }
//            }
//
//        }
//
//
//    }






    public String verifyMacBookMe294uaa(){
        Log4Test.info("Verify Apple MacBook Pro Retina 15\" (ME294UA/A)");
        listOfTitles = webDriver.findElements(listOfTitelsList);
        for (WebElement elem: listOfTitles){
            if (elem.getText().equals("Apple MacBook Pro Retina 15\" (ME294UA/A) Официальная гарантия!")){
                return elem.getText();
            }
        }
        return null;
    }





    public String findMacBookZ0pu002jeInCompareList(){
        Log4Test.info("findMacBookZ0pu002jeInCompareList");
        listOfCompare = webDriver.findElements(listOfCompareList);
        for (WebElement elem: listOfCompare){
            if (elem.getText().equals("Apple MacBook Pro Retina 15\" (Z0PU002JE) Официальная гарантия!")){
                return elem.getText();
            }
        }
        return null;
    }

    public String findMacBookMe294uaaInCompareList(){
        Log4Test.info("findMacBookMe294uaaInCompareList");
        WebDriverWait wait = new WebDriverWait(webDriver, 10);
        wait.until(ExpectedConditions.elementToBeClickable(secondItemInCompareBlockIsVisible));
        listOfCompare = webDriver.findElements(listOfCompareList);
        for (WebElement elem: listOfCompare){
            if (elem.getText().equals("Apple MacBook Pro Retina 15\" (ME294UA/A) Официальная гарантия!")){
                return elem.getText();
            }
        }
        return null;
    }







    public String verifyCompareList(){
        Log4Test.info("Verify compare list");
        verifyCompareList = webDriver.findElement(blockCompareList);
        return verifyCompareList.getText();



    }





    //Working method
    //public void clickToCompare(){
        //Log4Test.info("Click 'Compare' checkbox");
        //clickToCompare = webDriver.findElement(toCompare);
        //clickToCompare.click();

    //}
    public void clickToCompare() {
        Log4Test.info("Set Z0PU002JE checkbox");
        listOfTitles = webDriver.findElements(listOfTitelsList);
        for (WebElement elem : listOfTitles) {
            if (elem.getText().equals("Apple MacBook Pro Retina 15\" (Z0PU002JE) Официальная гарантия!")) {
                WebElement checkBox = elem.findElement(compareCheckBox);
                checkBox.click();
//                try {
//                    Thread.sleep(3000);
//                } catch (InterruptedException ie1) {
//                    ie1.printStackTrace();
//                }
                break;

            }
        }
    }

//            else{
//                WebElement nextItems = webDriver.findElement(By.xpath("//span[@class='hidden']/following-sibling::a"));
//                nextItems.click();
//                for (WebElement ele: listOfTitles){
//                    if (ele.getText().equals("Apple MacBook Pro Retina 15\" (Z0PU002JE) Официальная гарантия!")){
//                        WebElement checkBo = ele.findElement(compareCheckBox);
//                        checkBo.click();
//                        try {
//                        Thread.sleep(3000);
//                        }catch (InterruptedException ie1){
//                        ie1.printStackTrace();
//                        }
//
//                        break;
//
//                    }
//
//                }
//            }
//
//        }
//
//
//    }


    public String verifyApplePage (){
        Log4Test.info("Verify is that Apple page is opened");
        isAppleLocatorPresent = webDriver.findElement(isAppleLocator);
        return isAppleLocatorPresent.getText();

    }



    public void clickRating(){
        Log4Test.info("Click Rating DDL");
        clickRatingDdl = webDriver.findElement(ratingDdl);
        clickRatingDdl.click();
    }


    public void clickExpensiveToCheap(){
        Log4Test.info("Click on DDL 'from expensive to cheap' item");
        clickExpensiveToCheap = webDriver.findElement(expensiveToCheap);
        clickExpensiveToCheap.click();

    }
    //Working method
    //public String verifyMacBookZ0pu002je(){
      //  Log4Test.info("Verify that Apple MacBook Pro Retina 15\" (Z0PU002JE) is present");
        //clickVerifyMacBookZ0pu002je = webDriver.findElement(verifyMacBookZ0pu002je);
        //return clickVerifyMacBookZ0pu002je.getText();

    //}

    public String verifyMacBookZ0pu002je(){
        Log4Test.info("Verify that Apple MacBook Pro Retina 15\" (Z0PU002JE) is present");
        listOfTitles = webDriver.findElements(listOfTitelsList);
        for (WebElement elem: listOfTitles){
            if (elem.getText().equals("Apple MacBook Pro Retina 15\" (Z0PU002JE) Официальная гарантия!")){
                return elem.getText();
            }
        }
        return null;
    }

}
